/*
 *File name: hbox.c
 */

#include <gtk/gtk.h>
#include <glib.h>

extern int fanOnVal;
extern int fanOffVal;
extern int fanIntervalVal;

GtkWidget *fanOffAdjustment;
GtkWidget *fanOnAdjustment;
GtkWidget *intervalAdjustment;

/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
  g_print ("Quitting...\n");
  gtk_main_quit();
  return (FALSE);
}

void updateInterval(GtkAdjustment *adj)
{
	fanIntervalVal = adj->value;
}

void updateFanOn(GtkAdjustment *adj)
{
	fanOnVal = adj->value;
}

void updateFanOff(GtkAdjustment *adj)
{
	fanOffVal = adj->value;
}

void writeconfigExit(GtkWidget *widget, gpointer gdata)
{
  g_print("OK Button was clicked.\nwriting config, then destroy\n");
  writeConfig();
  gtk_main_quit();
}

void button_DumpConfigFile(GtkWidget *widget, gpointer gdata)
{
	g_print("dumpConfig Button\n");
	openConfig();
}

int main (int argc, char *argv[])
{
  /*-- Declare the GTK Widgets used in the program --*/
  GtkWidget *window;
  GtkWidget *dumpButton;
  GtkWidget *okButton;
  GtkWidget *cancelButton;
  GtkWidget *layoutTable;
  GtkWidget *fanOffLabel;
  GtkWidget *fanOffSpinner;
  GtkWidget *fanOnLabel;
  GtkWidget *fanOnSpinner;
  GtkWidget *intervalLabel;
  GtkWidget *intervalSpinner;

  /*--  Initialize GTK --*/
  gtk_init (&argc, &argv);

  /*-- Create the new window --*/
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  /*-- Create the labels --*/
  openConfig();

  fanOffLabel = gtk_label_new("Off Temp (C):");
  fanOffAdjustment = gtk_adjustment_new((float)fanOffVal, 40.0, 99.0, 1.0, 5.0, 0.0);

  fanOnLabel = gtk_label_new("On Temp (C):");
  fanOnAdjustment = gtk_adjustment_new((float)fanOnVal, 40.0, 99.0, 1.0, 5.0, 0.0);

  intervalLabel = gtk_label_new("Poll Interval:");
  intervalAdjustment = gtk_adjustment_new((float)fanIntervalVal, 1.0, 30.0, 1.0, 5.0, 0.0);
 
  /*-- Create some buttons to fill the box with --*/
  dumpButton = gtk_button_new_with_label("show config file");
  okButton = gtk_button_new_with_label("OK");
  cancelButton = gtk_button_new_with_label("cancel");
 
 fanOffSpinner = gtk_spin_button_new(fanOffAdjustment, 0, 0);
  gtk_spin_button_set_wrap(fanOffSpinner, FALSE);

  fanOnSpinner = gtk_spin_button_new(fanOnAdjustment, 0, 0);
  gtk_spin_button_set_wrap(fanOnSpinner, FALSE);
  
  intervalSpinner = gtk_spin_button_new(intervalAdjustment, 0, 0);
  gtk_spin_button_set_wrap(intervalSpinner, FALSE);

  /*-- Create the hbox --*/
  layoutTable = gtk_table_new(2, 6, FALSE);

  /*-- Connect the window to the destroyapp function  --*/
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

  /*-- Connect all the buttons to the button_was_clicked function --*/
  gtk_signal_connect(GTK_OBJECT(dumpButton), "clicked", GTK_SIGNAL_FUNC(button_DumpConfigFile), NULL);
  gtk_signal_connect(GTK_OBJECT(okButton), "clicked", GTK_SIGNAL_FUNC(writeconfigExit), NULL);
  gtk_signal_connect(GTK_OBJECT(cancelButton), "clicked", GTK_SIGNAL_FUNC(destroyapp), NULL);
  gtk_signal_connect(GTK_OBJECT(intervalAdjustment), "value-changed", GTK_SIGNAL_FUNC(updateInterval), NULL);
  gtk_signal_connect(GTK_OBJECT(fanOnAdjustment), "value-changed", GTK_SIGNAL_FUNC(updateFanOn), NULL);
  gtk_signal_connect(GTK_OBJECT(fanOffAdjustment), "value-changed", GTK_SIGNAL_FUNC(updateFanOff), NULL);

  /*-- Add all the buttons to the vbox --*/
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), fanOffLabel, 0, 1, 0, 1);
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), fanOffSpinner, 1, 2, 0, 1);
  
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), fanOnLabel, 2, 3, 0, 1);
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), fanOnSpinner, 3, 4, 0, 1);
   
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), intervalLabel, 4, 5, 0, 1);
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), intervalSpinner, 5, 6, 0, 1);
  
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), okButton, 2, 4, 1, 2);
  gtk_table_attach_defaults(GTK_TABLE(layoutTable), cancelButton, 4, 6, 1, 2);

  /*-- Add the button to the window --*/
  gtk_container_add(GTK_CONTAINER (window), layoutTable);

  /*-- Add a border to the window to give the buttons a little room --*/
  gtk_container_border_width (GTK_CONTAINER (window), 15);
  gtk_table_set_row_spacing(GTK_TABLE(layoutTable), 0, 20);
  gtk_table_set_col_spacing(GTK_TABLE(layoutTable), 1, 20);
  gtk_table_set_col_spacing(GTK_TABLE(layoutTable), 3, 20);

  /*-- Display the widgets --*/
  gtk_widget_show(layoutTable);

  //gtk_widget_show(dumpButton);
  gtk_widget_show(fanOffLabel);
  gtk_widget_show(fanOffSpinner);
  gtk_widget_show(fanOnLabel);
  gtk_widget_show(fanOnSpinner);
  gtk_widget_show(intervalLabel);
  gtk_widget_show(intervalSpinner);
  gtk_widget_show(okButton);
  gtk_widget_show(cancelButton);
  gtk_widget_show(window);

  /*-- Start the GTK event loop --*/
  gtk_main();

  /*-- Return 0 if exit is successful --*/
  return 0;
}
  
